feat: add as_veincidence_matrix() for vertex-edge incidence matrices
#2426
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implements vertex-edge incidence matrix computation as requested in #1122. The incidence matrix B has vertices as rows and edges as columns, encoding graph connectivity.
Implementation
as_veincidence_matrix(graph, attr = NULL, names = TRUE, sparse = igraph_opt("sparsematrices"))attrparameterUsage
Notes
as_biadjacency_matrix()convention rather thanlaplacian_matrix()styleOriginal prompt
This section details on the original issue you should resolve
<issue_title>Nice to have: vertex_edge_incidence_matrix().</issue_title>
<issue_description>What is the feature or improvement you would like to see?
Nice to have: function to calculate the vertex-edge incidence matrix of a graph.
20240118 - Added argument weights = NULL
20240119 - Added example graph, directed, multiple, weighted.
20240123 - Added example graph with self-loop.
Description
The vertex-edge incidence matrix of a graph.
Usage
as_ve_incidence_matrix <- function(
graph
, mode = c("all", "out", "in")
, loops
, multiple
, weights = NULL
, names = FALSE
, attr = "label"
, sparse = igraph_opt("sparsematrices")
)
Depending on personal taste, we can choose one of the following alternatives:
Arguments
graph
mode
loops
multiple
weights
For example, Wikipedia, wiki/Incidence_matrix, chapter Weighted graphs.
names
attr
sparse
Details
This function calculates the vertex-edge incidence matrix B of graph g.
If g is undirected, Bxe = 1 when vertex x is incident with edge e, 0 otherwise.
If g is directed, Bxe = −1,1,0 when vertex x is the head of edge e, the tail of e, or not on e, respectively.
If weights are taken into account, then -1, 1 becomes -w, w, where w is the weight of edge e.
Value
A possibly sparse matrix B with rows indexed by the vertices and columns indexed by the edges.
See Also
The function
laplacian_matrix()computes the out-degree Laplacian matrix. To allow the calculation of a symmetric Laplacian matrix, the function should be modified to support mode="all", in particular to ignore the orientation of edges. Currently, a workaround is to convert the graph to its undirected version.The function as_adjacency_matrix() includes weights using attr = "weight".
See also Github/igraph/rigraph/issue #906, as_adjacency_matrix() should use weights instead of attr parameter.
See also Github/igraph/rigraph/issues #1125, Treat self-loops in igraph functions consistently.
Examples